﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Model;


namespace MsDAL
{
   public partial  class I11073Base
    {		
		/// <summary>
        /// 增加一条数据
        /// </summary>
        public virtual  SqlApp Add(Model.I11073 i11073)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("insert into I11073(");
			strSql.Append("ID ,code ,NAME ,BARCODE ,family ,po ,kind ,brand ,pri ,dw ,dj ,BUYPRICE ,ACTIVESELLPRICE ,qdl ,tax ,cbfs )");
			strSql.Append(" values (");
			strSql.Append("@ID ,@code ,@NAME ,@BARCODE ,@family ,@po ,@kind ,@brand ,@pri ,@dw ,@dj ,@BUYPRICE ,@ACTIVESELLPRICE ,@qdl ,@tax ,@cbfs )");		

			SqlParameter[] parameters = {
				new SqlParameter("@ID",SqlDbType.NVarChar),
				new SqlParameter("@code",SqlDbType.NVarChar),
				new SqlParameter("@NAME",SqlDbType.NVarChar),
				new SqlParameter("@BARCODE",SqlDbType.NVarChar),
				new SqlParameter("@family",SqlDbType.NVarChar),
				new SqlParameter("@po",SqlDbType.NVarChar),
				new SqlParameter("@kind",SqlDbType.NVarChar),
				new SqlParameter("@brand",SqlDbType.NVarChar),
				new SqlParameter("@pri",SqlDbType.NVarChar),
				new SqlParameter("@dw",SqlDbType.NVarChar),
				new SqlParameter("@dj",SqlDbType.NVarChar),
				new SqlParameter("@BUYPRICE",SqlDbType.NVarChar),
				new SqlParameter("@ACTIVESELLPRICE",SqlDbType.NVarChar),
				new SqlParameter("@qdl",SqlDbType.NVarChar),
				new SqlParameter("@tax",SqlDbType.NVarChar),
				new SqlParameter("@cbfs",SqlDbType.NVarChar)
				};
			parameters[0].Value = i11073.ID;
			parameters[1].Value = i11073.Code;
			parameters[2].Value = i11073.NAME;
			parameters[3].Value = i11073.BARCODE;
			parameters[4].Value = i11073.Family;
			parameters[5].Value = i11073.Po;
			parameters[6].Value = i11073.Kind;
			parameters[7].Value = i11073.Brand;
			parameters[8].Value = i11073.Pri;
			parameters[9].Value = i11073.Dw;
			parameters[10].Value = i11073.Dj;
			parameters[11].Value = i11073.BUYPRICE;
			parameters[12].Value = i11073.ACTIVESELLPRICE;
			parameters[13].Value = i11073.Qdl;
			parameters[14].Value = i11073.Tax;
			parameters[15].Value = i11073.Cbfs;
			
			return new SqlApp(strSql.ToString(),new ArrayList(parameters), false, "数据保存失败！");
        }
	
        /// <summary>
        /// 判断某个值是否存在表中的某个字段
        /// </summary>
        /// <param name="Condition">条件字段名 例:A=@A</param>
        /// <param name="hs">参数和值 如hs["@A"]=123</param>
        /// <returns></returns>
        public virtual bool Exists(String condition, Hashtable hs)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(1) from I11073 ");
			if (!string.IsNullOrEmpty(condition))
				strSql.Append(" where " + condition);
			List<SqlParameter> list = DBHelper.GetListByHashtable(hs);
			if (list.Count > 0)
				return Convert.ToInt32(DBHelper.ExecuteScalar(strSql.ToString(), list.ToArray())) > 0;
			else
				return Convert.ToInt32(DBHelper.ExecuteScalar(strSql.ToString())) > 0;
        }

		
		
		 /// <summary>
        /// 得到Sql语句
        /// </summary>
        /// <param name="Condition">Sql语句的条件</param>
		/// <param name="OrderBy">排序条件，默认为String.Empty</param>
        /// <returns></returns>
        protected virtual String GetSql(string condition,string orderBy)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [ID] ,[code] ,[NAME] ,[BARCODE] ,[family] ,[po] ,[kind] ,[brand] ,[pri] ,[dw] ,[dj] ,[BUYPRICE] ,[ACTIVESELLPRICE] ,[qdl] ,[tax] ,[cbfs] ");
            strSql.Append(" FROM I11073 ");
 			
			if (!string.IsNullOrEmpty(condition)) strSql.Append(" where " + condition);				
			if(!string.IsNullOrEmpty(orderBy)) strSql.Append( " order by " + orderBy );
			
            return  strSql.ToString();
        }

        /// <summary>
        /// 根据条件和参数得到I11073对象
        /// </summary>
        /// <param name="Condition">Sql条件</param>
        /// <param name="hs">参数和值</param>
        /// <returns></returns>
        public virtual Model.I11073 GetModel(String Condition, Hashtable hs)
        {
			String sql = GetSql(Condition,String.Empty);
			DataTable dt = DBHelper.GetTableByCondition(sql,  hs);
			return (dt.Rows.Count > 0) ? Model.I11073.GetI11073ListByDataTable(dt)[0] : null;
        }

		
		public virtual DataTable GetList(string c,Hashtable hs)
		{
			string sql = GetSql(c,null); //得到SQL语句
			//返回结果集
			return DBHelper.GetData(sql,DBHelper.GetListByHashtable(hs).ToArray());
		}
    }
}
